//
//  CollectionCell.m
//  Meng
//
//  Created by dllo on 15/9/29.
//  Copyright (c) 2015年 ljw. All rights reserved.
//

#import "CollectionCell.h"
#import "TableViewCell.h"
#import "HomeModel.h"
#import "ADView.h"
#import "HomeWebViewController.h"
#import <MJRefresh.h>
@implementation CollectionCell
- (void)dealloc
{
    [_tableView release];
    
    [super dealloc];
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.contentView.frame.size.width, self.contentView.frame.size.height) style:UITableViewStyleGrouped];
        [self.contentView addSubview:self.tableView];
        [_tableView release];
        self.tableView.delegate = self;
        self.tableView.dataSource =self;
        [self.tableView registerClass:[TableViewCell class] forCellReuseIdentifier:@"reuse2"];
        
        self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [self.arr removeAllObjects];
            [self dataHandle:self.str];
            NSLog(@"1");
        }];
        [self.tableView.header beginRefreshing];
        
        self.tableView.footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
            if ([[self.arr lastObject] next_url] != nil) {
                [self dataHandle:[[self.arr lastObject] next_url]];
            }
            NSLog(@"2");
        }];
        [self.tableView.footer beginRefreshing];
    }
    return self;
}
- (void)setModelStr:(NSString *)str
{
    self.str = str;
    self.arr = [NSMutableArray array];
    [self dataHandle:str];
}
- (void)dataHandle:(NSString *)str
{
    NSString *arrStr = [[[[str componentsSeparatedByString:other] lastObject] componentsSeparatedByString:other1] objectAtIndex:0];
    NSString *string = [NSString stringWithFormat:@"CashArr%@.plist", arrStr];
    NSString *arrPath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:string];
   
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSMutableDictionary *dic = [responseObject objectForKey:@"data"];
        NSMutableArray *arr = [dic objectForKey:@"items"];
        NSMutableDictionary *dictionary = [dic objectForKey:@"paging"];
        for (NSMutableDictionary *dic1 in arr) {
            HomeModel *model = [[HomeModel alloc] init];
            [model setValuesForKeysWithDictionary:dictionary];
            [model setValuesForKeysWithDictionary:dic1];
            [self.arr addObject:model];
            [model release];
        }
        [self.tableView reloadData];
        [self.tableView.header endRefreshing];
        [self.tableView.footer endRefreshing];
        [arr writeToFile:arrPath atomically:YES];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSArray *arr = [NSArray arrayWithContentsOfFile:arrPath];
        for (NSMutableDictionary *dic1 in arr) {
            HomeModel *model = [[HomeModel alloc] init];
            [model setValuesForKeysWithDictionary:dic1];
            [self.arr addObject:model];
            [model release];
        }
        [self.tableView reloadData];
        [self.tableView.header endRefreshing];
        [self.tableView.footer endRefreshing];

    }];
}
- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes
{
    self.tableView.frame = CGRectMake(0, 0, self.contentView.frame.size.width, self.contentView.frame.size.height);
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse2"];
    if (self.arr.count != 0) {
        HomeModel *model = self.arr[indexPath.row];
        cell.homemodel = model;
    }
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 250 * HEIGHT;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (self.ret == YES) {
        return 250 * HEIGHT;
    } else {
        return 0.0001;
    }
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (self.ret == YES) {
        ADView *view = [[ADView alloc] initWithFrame:CGRectMake(0, 0, self.contentView.frame.size.width, 250 * HEIGHT) dataArr:self.arrS];
        return view;
    } else {
        return nil;
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    HomeModel *home = [self.arr objectAtIndex:indexPath.row];
    //通知 传值 应为 这不是在vc里写的 导致无法push过去 所以写了通知 发送通知 然后在vc里push
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center postNotificationName:@"change" object:home.url userInfo:nil];
    
}
@end
